package com.quectel.cms.controller.prewarning;

import com.quectel.cms.controller.BaseController;
import com.quectel.constant.global.SystemConstants;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.core.module.prewarning.dto.PreWarningRecordHistoryDto;
import com.quectel.core.module.prewarning.service.PreWarningRecordHistoryService;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2021-11-30 15:15:57
 */
@RestController
@RequestMapping("preWarningRecordHistory")
@Tag(name = "PreWarningRecordHistoryController", description = "人车预警历史记录相关api")
public class PreWarningRecordHistoryController extends BaseController {

    @DubboReference
    private PreWarningRecordHistoryService preWarningRecordHistoryService;

    /**
     * 人车预警记录列表，支持分页
     */
    @GetMapping("list")
    @RequirePermissions(values = "preWarningRecordHistory:list")
    @Operation(summary = "人车预警记录列表，支持分页")
    public Response<Page<PreWarningRecordHistoryDto>> list(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize
    ) {
        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        super.fillAuthorityData(request.getParams());
        int total = preWarningRecordHistoryService.queryTotal(request.getParams());

        List<PreWarningRecordHistoryDto> list = null;
        if (total > 0) {
            list = preWarningRecordHistoryService.queryList(request.getParams());
        } else {
            list = new ArrayList<>();
        }

        Page<PreWarningRecordHistoryDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<PreWarningRecordHistoryDto>>ok().wrap(page);
    }


    /**
     * 查询人车预警单条记录
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "preWarningRecordHistory:info")
    @Operation(summary = "查询人车预警单条历史记录")
    public Response<PreWarningRecordHistoryDto> info(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (preWarningRecordHistoryService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        PreWarningRecordHistoryDto preWarningRecordDto = preWarningRecordHistoryService.selectById(id);

        return Response.<PreWarningRecordHistoryDto>ok().wrap(preWarningRecordDto);
    }
}
